package com.hongshan.wallet.http

import android.annotation.SuppressLint
import com.hongshan.wallet.BuildConfig
import com.hongshan.wallet.Constant
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.net.Proxy
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager


/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.util
 * @author   : Administrator
 * @data     : 2019/3/18
 * @email    : zhangxx@baihang-china.com
 */
object HttpClient {

    const val CONNECT_TIME_OUT = 10 * Constant.Time.SECOND
    const val READ_TIME_OUT = 10 * Constant.Time.SECOND
    const val WRITE_TIME_OUT = 10 * Constant.Time.SECOND

    const val JSON_HEAD = "Content-Type:application/json"

    val client: OkHttpClient by lazy {
        val logInterceptor = HttpLoggingInterceptor()
        if (BuildConfig.DEBUG) {
            logInterceptor.level = HttpLoggingInterceptor.Level.BODY
        } else {
            logInterceptor.level = HttpLoggingInterceptor.Level.NONE
        }
        logInterceptor.redactHeader("zhang")
        val builder = OkHttpClient.Builder()
            .connectTimeout(CONNECT_TIME_OUT, TimeUnit.MILLISECONDS)
            .readTimeout(READ_TIME_OUT, TimeUnit.MILLISECONDS)
            .writeTimeout(WRITE_TIME_OUT, TimeUnit.MILLISECONDS)
            .addInterceptor(HttpInterceptor)
            .addNetworkInterceptor(logInterceptor)
        if (BuildConfig.DEBUG) {
            val xtm = object : X509TrustManager {
                @SuppressLint("TrustAllX509TrustManager")
                override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
                @SuppressLint("TrustAllX509TrustManager")
                override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}

                override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
            }

            try {
                val sslContext = SSLContext.getInstance("SSL")
                sslContext!!.init(null, arrayOf<TrustManager>(xtm), SecureRandom())
                val DO_NOT_VERIFY = HostnameVerifier { _, _ -> true }
                builder.sslSocketFactory(sslContext.socketFactory)
                builder.hostnameVerifier(DO_NOT_VERIFY)
            } catch (e: Exception) {
                e.printStackTrace()
            }

        } else {
            builder.proxy(Proxy.NO_PROXY)
        }
        return@lazy builder.build()
    }

    val httpApi =
        Retrofit.Builder()
            .client(client)
            .baseUrl(Constant.HttpApi.BASE_URL)
//            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(HttpApi::class.java)
}